﻿namespace GSC.WebControls
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.ComponentModel.Design;
    using System.Web.UI.HtmlControls;
    /// <summary>
    /// 
    /// </summary>
    internal sealed class BackgroundHeadControlDesigner : DataBoundControlDesigner
    {
        /// <summary>
        /// 
        /// </summary>
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection al = base.ActionLists;
                //al.Add(new HeadActionList(this.Component, this));
                return al;
            }
        }
        protected override void OnDataSourceChanged(bool forceUpdateView)
        {
            base.OnDataSourceChanged(forceUpdateView);
        }
        protected override void DataBind(BaseDataBoundControl dataBoundControl)
        {
            base.DataBind(dataBoundControl);
        }
        protected override void PostFilterProperties(System.Collections.IDictionary properties)
        {
            base.PostFilterProperties(properties);
            if (string.IsNullOrEmpty(this.DataSourceID))
            {
                if (!properties.Contains("Items"))
                {
                    PropertyDescriptor pdItems = TypeDescriptor.CreateProperty(
                        typeof(BackgroundHeadControlDesigner),
                        "Items",
                        typeof(BackgroundHeadControlItemCollection),
                        new Attribute[] { new DesignOnlyAttribute(true) });
                    properties.Add("Items", pdItems);
                }
            }
            else
            {
                if (properties.Contains("Items"))
                    properties.Remove("Items");
            }
        }
        BackgroundHeadControl _headControl;
        private const string Items = "Items";
        private const string Logo = "Logo";
        private const string Logout = "HeadLogout";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Component"></param>
        public override void Initialize(IComponent Component)
        {
            base.Initialize(Component);
            this._headControl = (BackgroundHeadControl)Component;
            //SetViewFlags(ViewFlags.DesignTimeHtmlRequiresLoadComplete, true);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }
        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection collection = new TemplateGroupCollection();
                TemplateGroup group;
                TemplateDefinition template;
                BackgroundHeadControl control;
                control = (BackgroundHeadControl)Component;
                //
                group = new TemplateGroup("Logo");
                template = new TemplateDefinition(this, "LogoTemplate", control, "LogoTemplate", false);
                group.AddTemplateDefinition(template);
                collection.Add(group);
                //
                group = new TemplateGroup("HeadLogout");
                template = new TemplateDefinition(this, "LogoutTemplate", control, "LogoutTemplate", false);
                group.AddTemplateDefinition(template);
                collection.Add(group);
                return collection;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override bool AllowResize
        {
            get
            {
                return false;
            }
        }
        protected override bool ConnectToDataSource()
        {
            return base.ConnectToDataSource();
        }
        protected override void CreateDataSource()
        {
            base.CreateDataSource();
        }
        public override void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
        {
            base.OnComponentChanged(sender, ce);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string GetDesignTimeHtml()
        {
            try
            {
                //
                //this.CreateChildControls();
                if (GetDesignTimeDataSource() != null && !string.IsNullOrEmpty(this._headControl.DataSourceID))
                {
                    int index = 0;
                    this._headControl.Items.Clear();
                    //键值
                    string _DataTextField = this._headControl.DataTextField;
                    string _DataCommandArgumentField = this._headControl.DataCommandArgumentField;
                    string _DataCommandNameField = this._headControl.DataCommandNameField;
                    //
                    foreach (object dataItem in this.GetDesignTimeDataSource())
                    {
                        BackgroundHeadControlItem item = new BackgroundHeadControlItem();
                        //
                        if (!string.IsNullOrEmpty(_DataTextField))
                        {
                            item.Text = DataBinder.GetPropertyValue(dataItem, _DataTextField, null);
                        }
                        //
                        if (!string.IsNullOrEmpty(_DataCommandArgumentField))
                        {
                            item.CommandArgument = DataBinder.GetPropertyValue(dataItem, _DataCommandArgumentField, null);
                        }
                        //
                        if (!string.IsNullOrEmpty(_DataCommandNameField))
                        {
                            item.CommandName = DataBinder.GetPropertyValue(dataItem, _DataCommandNameField, null);
                        }
                        this._headControl.Items.Add(item);
                        index++;
                    }
                }
                //
                ((ICompositeControlDesignerAccessor)_headControl).RecreateChildControls();
                foreach (BackgroundHeadControlItem item in this._headControl.Items)
                {
                    ((ICompositeControlDesignerAccessor)item).RecreateChildControls();
                }
                //
                string l = System.Threading.Thread.CurrentThread.CurrentCulture.Name.Replace("-", "_");
                Panel panel = new Panel();
                Table table = new Table();
                table.CellPadding = 0;
                table.CellSpacing = 0;
                table.BackColor = _headControl.BackColor;
                table.BorderColor = _headControl.BorderColor;
                table.BorderStyle = _headControl.BorderStyle;
                table.BorderWidth = _headControl.BorderWidth;
                table.Width = _headControl.Width;
                table.Height = _headControl.Height;
                table.Style["table-layout"] = "fixed";
                table.Style["background-repeat"] = "repeat-x";
                if (string.IsNullOrEmpty(_headControl.BackImage))
                    table.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("bg.gif")) + ")";
                else
                    table.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.BackImage) + ")";
                panel.Controls.Add(table);
                TableRow row = new TableRow();
                row.Height = _headControl.Height;
                table.Rows.Add(row);
                //
                TableCell LogoCell = new TableCell();
                LogoCell.Height = _headControl.Height;
                LogoCell.Width = _headControl.LogoCellWidth;
                LogoCell.Style["overflow"] = "hidden";
                LogoCell.Style["background-repeat"] = "no-repeat";
                this._headControl.LogoCell.Style["text-align"] = "left";
                if (string.IsNullOrEmpty(_headControl.LogoBackImage))
                    LogoCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("logobg.gif")) + ")";
                else
                    LogoCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.LogoBackImage) + ")";
                row.Cells.Add(LogoCell);
                foreach (Control item in _headControl.LogoCell.Controls)
                {
                    LogoCell.Controls.Add(item);
                }
                //
                List<TableCell> cells = new List<TableCell>();
                foreach (TableCell cell in _headControl.ItemCells)
                {
                    TableCell cellItem = new TableCell();
                    cellItem.Height = _headControl.Height;
                    cellItem.Width = _headControl.ItemWidth;
                    cellItem.Style["text-align"] = "left";
                    cellItem.Style["overflow"] = "hidden";
                    cellItem.Style["background-repeat"] = "repeat-x";
                    cellItem.Style["background-position"] = "left top";
                    if (string.IsNullOrEmpty(_headControl.ItemBackImage))
                        cellItem.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("itembg.gif")) + ")";
                    else
                        cellItem.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.ItemBackImage) + ")";
                    row.Cells.Add(cellItem);
                    cells.Add(cellItem);
                }
                //
                for (int i = 0; i < _headControl.Items.Count; i++)
                {
                    Panel cellPanel = new Panel();
                    cellPanel.Height = _headControl.Height;
                    cellPanel.Width = _headControl.ItemWidth;
                    cellPanel.Style["text-align"] = "center";
                    cellPanel.Style["overflow"] = "hidden";
                    cellPanel.Style["background-repeat"] = "no-repeat";
                    cellPanel.Style["background-position"] = "left top";
                    if (string.IsNullOrEmpty(_headControl.SecludeBackImage))
                        cellPanel.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("seclude.gif")) + ")";
                    else
                        cellPanel.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.SecludeBackImage) + ")";
                    Panel TopPanel = new Panel();
                    TopPanel.Height = _headControl.ItemPaddingTop;
                    cellPanel.Controls.Add(TopPanel);
                    //
                    LinkButton linkButton = new LinkButton();
                    linkButton.Text = _headControl.Items[i].Text;
                    linkButton.Font.CopyFrom(_headControl.Items[i].Font);
                    linkButton.ForeColor = _headControl.Items[i].ForeColor;
                    linkButton.ToolTip = _headControl.Items[i].ToolTip;
                    linkButton.PostBackUrl = _headControl.Items[i].PostBackUrl;
                    cellPanel.Controls.Add(linkButton);
                    cells[i].Controls.Add(cellPanel);
                }
                //
                TableCell LogoutCell = new TableCell();
                LogoutCell.Height = _headControl.Height;
                LogoutCell.Style["width"] = "auto";
                //LogoutCell.Style["word-wrap"] = "keep-all";
                LogoutCell.Style["background-repeat"] = "no-repeat";
                LogoutCell.Style["background-position"] = "left top";
                LogoutCell.Style["vertical-align"] = "middle";
                LogoutCell.Style["text-align"] = "right";
                LogoutCell.Style["padding-right"] = "8px";
                LogoutCell.Style["padding-left"] = "8px";
                LogoutCell.Style["overflow"] = "hidden";
                if (string.IsNullOrEmpty(_headControl.SecludeBackImage))
                    LogoutCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("seclude.gif")) + ")";
                else
                    LogoutCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.SecludeBackImage) + ")";
                //
                foreach (Control item in _headControl.LogoutCell.Controls)
                {
                    LogoutCell.Controls.Add(item);
                }
                //
                foreach (Control item in LogoutCell.Controls)
                {
                    if (item is ImageButton && ((ImageButton)item).CommandName == "HeadLogout")
                    {
                        if (string.IsNullOrEmpty(this._headControl.LogoutImageUrl))
                        {
                            ((ImageButton)item).ImageUrl = this._headControl.GetWebResourceUrl(l + ".logout.gif");
                        }
                        else
                        {
                            ((ImageButton)item).ImageUrl = this._headControl.LogoutImageUrl;
                        }
                        break;
                    }
                }
                row.Cells.Add(LogoutCell);
                /*
                //
                _headControl.Table.CellPadding = 0;
                _headControl.Table.CellSpacing = 0;
                _headControl.Table.BackColor = _headControl.BackColor;
                _headControl.Table.BorderColor = _headControl.BorderColor;
                _headControl.Table.BorderStyle = _headControl.BorderStyle;
                _headControl.Table.BorderWidth = _headControl.BorderWidth;
                _headControl.Table.Width = _headControl.Width;
                _headControl.Table.Height = _headControl.Height;
                this._headControl.Table.Style["table-layout"] = "fixed";
                this._headControl.Table.Style["background-repeat"] = "repeat-x";
                if (string.IsNullOrEmpty(_headControl.BackImage))
                    this._headControl.Table.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("bg.gif")) + ")";
                else
                    this._headControl.Table.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.BackImage) + ")";
                //
                _headControl.Row.Height = _headControl.Height;
                //
                this._headControl.LogoCell.Height = _headControl.Height;
                this._headControl.LogoCell.Width = Unit.Pixel(188);
                this._headControl.LogoCell.Style["overflow"] = "hidden";
                this._headControl.LogoCell.Style["background-repeat"] = "no-repeat";
                this._headControl.LogoCell.Style["text-align"] = "_left";
                if (string.IsNullOrEmpty(_headControl.LogoBackImage))
                    this._headControl.LogoCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("logobg.gif")) + ")";
                else
                    this._headControl.LogoCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.LogoBackImage) + ")";
                //
                foreach (TableCell cell in _headControl.ItemCells)
                {
                    cell.Height = _headControl.Height;
                    cell.Width = _headControl.ItemWidth;
                    cell.Style["text-align"] = "_left";
                    cell.Style["overflow"] = "hidden";
                    cell.Style["background-repeat"] = "repeat-x";
                    cell.Style["background-position"] = "_left _top";
                    if (string.IsNullOrEmpty(_headControl.ItemBackImage))
                        cell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("itembg.gif")) + ")";
                    else
                        cell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.ItemBackImage) + ")";
                }
                foreach (HeadItem item in _headControl.Items)
                {
                    item.LinkButton.Text = item.Text;
                    item.LinkButton.Font.CopyFrom(item.Font);
                    item.LinkButton.ForeColor = item.ForeColor;
                    item.LinkButton.ToolTip = item.ToolTip;
                    item.LinkButton.PostBackUrl = item.PostBackUrl;
                    //
                    item.Height = _headControl.Height;
                    item.Width = _headControl.ItemWidth;
                    item.Style["text-align"] = "center";
                    item.Style["overflow"] = "hidden";
                    item.Style["background-repeat"] = "no-repeat";
                    item.Style["background-position"] = "_left _top";
                    if (string.IsNullOrEmpty(_headControl.SecludeBackImage))
                        item.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("seclude.gif")) + ")";
                    else
                        item.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.SecludeBackImage) + ")";
                    //
                    item.TopPanel.Height = _headControl.ItemPaddingTop;
                    //
                }
                //
                this._headControl.LogoutCell.Height = _headControl.Height;
                this._headControl.LogoutCell.Style["width"] = "auto";
                //this._headControl.LogoutCell.Style["word-wrap"] = "keep-all";
                this._headControl.LogoutCell.Style["background-repeat"] = "no-repeat";
                this._headControl.LogoutCell.Style["background-position"] = "_left _top";
                this._headControl.LogoutCell.Style["vertical-align"] = "middle";
                this._headControl.LogoutCell.Style["text-align"] = "_right";
                this._headControl.LogoutCell.Style["padding-_right"] = "8px";
                this._headControl.LogoutCell.Style["padding-_left"] = "8px";
                this._headControl.LogoutCell.Style["overflow"] = "hidden";
                if (string.IsNullOrEmpty(_headControl.SecludeBackImage))
                    this._headControl.LogoutCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.GetWebResourceUrl("seclude.gif")) + ")";
                else
                    this._headControl.LogoutCell.Style["background-image"] = "url(" + _headControl.ResolveClientUrl(_headControl.SecludeBackImage) + ")";
                //
                foreach (Control item in this._headControl.LogoutCell.Controls)
                {
                    if (item is ImageButton && ((ImageButton)item).CommandName == "HeadLogout")
                    {
                        if (string.IsNullOrEmpty(this._headControl.LogoutImageUrl))
                        {
                            ((ImageButton)item).ImageUrl = this._headControl.GetWebResourceUrl(l + ".logout.gif");
                        }
                        else
                        {
                            ((ImageButton)item).ImageUrl = this._headControl.LogoutImageUrl;
                        }
                        break;
                    }
                }
                //
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);;
                this._headControl.RenderControl(hw);
                 */
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw); ;
                panel.RenderControl(hw);
                return sw.ToString();
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}